package otaku.gateway;

import io.netty.handler.codec.http.DefaultHttpHeaders;
import otaku.gateway.config.ServerConfig;
import otaku.gateway.core.AddHeaderDownstreamResponseFilter;
import otaku.gateway.core.AddHeaderUpstreamRequestFilter;
import otaku.gateway.core.NettyHttpServer;
import otaku.gateway.core.PathUpstreamRequestMatcher;

import java.net.InetSocketAddress;
import java.util.List;

public class App {
    public static void main(String[] args) {
        //启动一个反向代理服务器，代理到localhost:81，前提是请求url以/index.html开头
        ServerConfig serverConfig = new ServerConfig();
        serverConfig.setLocalAddress("0.0.0.0");
        serverConfig.setPort(80);
        serverConfig.setUpstreamRequestMatchers(List.of(
                new PathUpstreamRequestMatcher("/index.html", List.of(new InetSocketAddress("localhost", 81)))
        ));
        DefaultHttpHeaders headers = new DefaultHttpHeaders();
        headers.add("x-proxy", "true");
        serverConfig.setUpstreamRequestFilters(List.of(
                new AddHeaderUpstreamRequestFilter(headers)
        ));
        DefaultHttpHeaders responseHeaders = new DefaultHttpHeaders();
        responseHeaders.add("x-downstream-response", "true");
        serverConfig.setDownstreamResponseFilters(List.of(
                new AddHeaderDownstreamResponseFilter(responseHeaders)
        ));
        NettyHttpServer server = new NettyHttpServer(serverConfig);
        server.start();
    }
}
